package com.ayoka.parenting.widget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class NotificationDialog extends BaseDialogFragment {

    public static NotificationDialog newInstance(Builder builder) {
        NotificationDialog dialog = new NotificationDialog();
        Bundle bundle = getArgumentBundle(builder);
        bundle.putString("TITLE", builder.title);
        bundle.putCharSequence("MESSAGE", builder.message);
        bundle.putString("NEGATIVE_TEXT", builder.negativeText);
        bundle.putString("POSITIVE_TEXT", builder.positiveText);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            TextView tvTitle = (TextView) container.findViewById(R.id.tv_dialog_title);
            if (!TextUtils.isEmpty(bundle.getString("TITLE"))) {
                tvTitle.setText(getArguments().getString("TITLE"));
            }
            TextView tvMessage = (TextView) container.findViewById(R.id.tv_dialog_message);
            if (!TextUtils.isEmpty(bundle.getCharSequence("MESSAGE"))) {
                tvMessage.setText(getArguments().getCharSequence("MESSAGE"));
            }
            Button btnNegative = (Button) container.findViewById(R.id.btn_negative);
            Button btnPositive = (Button) container.findViewById(R.id.btn_positive);

            if (!TextUtils.isEmpty(bundle.getString("NEGATIVE_TEXT"))) {
                btnNegative.setText(bundle.getString("NEGATIVE_TEXT"));
            }
            if (!TextUtils.isEmpty(bundle.getString("POSITIVE_TEXT"))) {
                btnPositive.setText(bundle.getString("POSITIVE_TEXT"));
            }
            btnNegative.setOnClickListener(new CloseClickListener());
            btnPositive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDialogResultListener != null) {
                        mDialogResultListener.result(NotificationDialog.this, "CONFIRM");
                    }
                }
            });
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_notification;
    }



    public static class Builder extends BaseDialogBuilder<Builder, NotificationDialog> {

        private String title;

        private CharSequence message;

        private String negativeText;

        private String positiveText;


        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }

        public Builder setPositiveText(String positiveText) {
            this.positiveText = positiveText;
            return this;
        }

        public Builder setNegativeText(String negativeText) {
            this.negativeText = negativeText;
            return this;
        }

        @Override
        public NotificationDialog build() {
            return NotificationDialog.newInstance(this);
        }
    }

}